package action.zpcj;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.multipart.MultipartFile;  
import org.springframework.web.multipart.MultipartHttpServletRequest;  
import org.springframework.web.multipart.commons.CommonsMultipartResolver;  
import services.zpcj.ZpcjServices;
import com.Const;
import com.syslog.SysLogServices;
import com.util.Component;
import com.util.GetFileMd5;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 照片上传
* @author bjh
* @version Fri May 06 17:59:17 CST 2016 gjx
* @since 1.0
*/
@Controller
@RequestMapping("pic.do") 
public class UploadAction {
	ZpcjServices ss = new ZpcjServices();
	SysLogServices log = SysLogServices.getInstance();
    @RequestMapping(params = "method=upload")
    public void upload(HttpServletRequest request, HttpServletResponse response) throws Exception {
	    String aac001 = Component.getQuest("key", request);
	    String aae011 = Component.getSession("userid", request);
	    String str = "";
	    //创建一个通用的多部分解析器  
	    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
	    //判断 request 是否有文件上传,即多部分请求  
	    if(multipartResolver.isMultipart(request)) {
	    	//转换成多部分request
	    	MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
	    	//取得request中的所有文件名  
	    	Iterator<String> iter = multiRequest.getFileNames();
	    	while(iter.hasNext()) { 
	    		//取得上传文件  
	    		MultipartFile file = multiRequest.getFile(iter.next());
	    		if(file != null) {
	    			//取得当前上传文件的文件名称 
	    			String myFileName = file.getOriginalFilename();
	    			//String fileName = aac001 + "." + "JPG";
	    			//String
	    			//如果名称不为"",说明该文件存在，否则说明该文件不存在  
	    			if(myFileName.trim() != "") {
	    				//定义上传路径 
	    				Date de =new Date();
	    				SimpleDateFormat sp =new  SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
	    				String s_time=sp.format(de);
	    				String picurl=s_time.substring(0,4)+s_time.substring(5,7)+s_time.substring(8,10);
	    				int radNum = (int)(Math.random() * 100000);
	    				String encodeFileName = s_time + "_" + radNum + "."+"JPG";
	    				String path = Const.server_path + "/uploadPic/"+picurl+"/" + encodeFileName;
	    				File localFile = new File(path);			    				
	    				if(!localFile.exists()) {
	    					localFile.mkdirs();
	    				}	
	    				file.transferTo(localFile); 
	    				long maxsize = 200*1024;
	    				long minsize = 6*1024;
	    				long size = localFile.length();
	    				if(size < minsize || size > maxsize)
	    				{	    					
	    					str = "2";
	    					Component.print(str, response);
	    					return;
	    				}
	    				String md5 = GetFileMd5.getFileMD5String(localFile);
	    				
	    				HashMap row1 = new HashMap();
	    				row1.put("aac001", aac001);
	    				HashMap row2 = new HashMap();                   
	    				row2.put("aac001", aac001); 
	    				row2.put("aaz755", md5); 
	    				row2.put("aaz754", (int)size);
	    				row2.put("aae011", aae011);                
	    				str = ss.insertUnit(row1,aac001,row2,path) + "";
	    				HashMap obj=new HashMap();
	    				if(str.equals(0)) {
				       		obj.put("domainid", "SBK");
				       		obj.put("log_type", "T02");
				       		obj.put("userid", aae011);
				       		obj.put("rwh", Component.getSession("rwid", request));
				       		obj.put("content", "照片采集：社保卡号" + aac001 + "照片名" + myFileName);
				       		obj.put("czqd", "Q1");
				       		obj.put("czrlb", "L01"); 
				       		obj.put("datain", "0");
				       		obj.put("dataout", "error");
				       		log.sysLogMap(obj, request);
	    				} else {
			        	    obj.put("domainid", "SBK");
			          		obj.put("log_type", "T02");
			          		obj.put("userid", aae011);
			          		obj.put("rwh", Component.getSession("rwid", request));
			          		obj.put("content", "照片采集：社保卡号" + aac001 + "照片名" + myFileName);
			          		obj.put("czqd", "Q1");
			          		obj.put("czrlb", "L01"); 
			          		obj.put("datain", "1");
			          		obj.put("dataout", "success");
			          		log.sysLogMap(obj, request);
	    				}
	    			}
	    		}
	    	}
	           Component.print(str, response);
	    }
   }
}
    

